package com.eightbears.beboy.ui.chat.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eightbears.beboy.R;
import com.eightbears.beboy.adapter.ComplaintAdapter;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.base.view.layout.MyLinearLayoutManager;
import com.eightbears.beboy.entity.ComplaintEntity;
import com.eightbears.beboy.entity.status.Status_Error;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.URL_RESET_USERDATA;
import static com.eightbears.beboy.ui.chat.ui.fragment.ChatFragment.PARAMS_TO_CHAT_USERNAME;

public class ChatComplaintActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.rv_list)
    RecyclerView rv_list;
    @BindView(R.id.et_text)
    EditText et_text;
    @BindView(R.id.sw_finish)
    SwipeBackLayout sw_finish;

    private String userId;
    private String content;

    private ComplaintAdapter mComplaintAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_conplaint);

        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        tv_title.setText(R.string.text_complaint);
    }

    @Override
    protected void initData() {
        rv_list.setLayoutManager(new MyLinearLayoutManager(this));
        mComplaintAdapter = new ComplaintAdapter();
        mComplaintAdapter.setNewData(ComplaintEntity.getComplanints());
        rv_list.setAdapter(mComplaintAdapter);
        userId = getIntent().getStringExtra(PARAMS_TO_CHAT_USERNAME);
        content = "发送不适当内容对我进行骚扰";
    }

    @Override
    protected void initEvent() {
        mComplaintAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                ComplaintEntity complanintEntity = (ComplaintEntity) adapter.getData().get(position);
                content = complanintEntity.getName();
                if(!et_text.getText().toString().trim().equals("")){
                    content += ":" + et_text.getText().toString();
                }
                mComplaintAdapter.setIv_icon(position);
            }
        });

        sw_finish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
    }

    @OnClick(R.id.btn_report) void report(){
        sendReport();
    }

    @OnClick(R.id.ll_back) void back(){
        finish();
    }

    private void sendReport() {
        String url = URL_RESET_USERDATA + userId + "/report";
        OkHttpUtils.post().url(url)
              .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .addParams("reason", content)
                .build().execute(new StringCallback() {

            @Override
            public void onResponse(String response) {
                if(response.contains("200")){
                    ShowToast.showShortToast(getString(R.string.text_complaint_success));
                    finish();
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                ErrorUtils.alertError(ChatComplaintActivity.this, status_error);
            }
        });
    }
}
